#include"head.h"

typedef struct ListNode {
	int val;
	struct ListNode *next;
}SLTNode;

SLTNode* BuySLTNode(int a)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->val = a;
	newnode->next = NULL;
	return newnode;
}

struct ListNode* reverseList(struct ListNode* head){
	SLTNode* cor = head;
	SLTNode* cornext1 = head;
	if (cor == NULL || cor->next == NULL)
	{
		return head;
	}
	SLTNode* cornext = cor->next;
	head->next = NULL;
	if (cornext->next != NULL)
	{
		cornext1 = cornext->next;
	}
	else
	{
		cornext->next = head;
		head = cornext;
		return head;
	}
	while (cornext)
	{
		cornext->next = cor;
		cor = cornext;
		cornext = cornext1;
		if (cornext != NULL)
		{
			cornext1 = cornext->next;
		}
		else
		{
			head = cor;
		}
	}
	return head;
}
void SLTPrint(SLTNode* head)
{
	SLTNode* cur = head;
	while (cur != NULL)
	{
		printf("%d->", cur->val);
		cur = cur->next;
	}
	printf("NULL\n");
}

int main()
{
	SLTNode* n1 = BuySLTNode(1);
	SLTNode* n2 = BuySLTNode(2);
	SLTNode* n3 = BuySLTNode(3);
	SLTNode* n4 = BuySLTNode(4);
	SLTNode* n5 = BuySLTNode(5);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = n5;
	n5->next = NULL;
	SLTPrint(n1);
	struct ListNode* s = reverseList(n1);
	SLTPrint(s);
	system("pause");
	return 0;
}